package com.mkx.multitier;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 资源使用情况类，用于统计各层次的资源使用情况
 */
public class ResourceUsage {
    private final AtomicLong threadCount = new AtomicLong(0);
    private final AtomicLong queueSize = new AtomicLong(0);
    private final AtomicLong activeTasks = new AtomicLong(0);
    private final AtomicLong completedTasks = new AtomicLong(0);
    private final AtomicLong failedTasks = new AtomicLong(0);
    private final AtomicLong cpuUsage = new AtomicLong(0); // 0-100
    
    public void incrementThreadCount() {
        threadCount.incrementAndGet();
    }
    
    public void decrementThreadCount() {
        threadCount.decrementAndGet();
    }
    
    public void setThreadCount(long count) {
        threadCount.set(count);
    }
    
    public long getThreadCount() {
        return threadCount.get();
    }
    
    public void incrementQueueSize() {
        queueSize.incrementAndGet();
    }
    
    public void decrementQueueSize() {
        queueSize.decrementAndGet();
    }
    
    public void setQueueSize(long size) {
        queueSize.set(size);
    }
    
    public long getQueueSize() {
        return queueSize.get();
    }
    
    public void incrementActiveTasks() {
        activeTasks.incrementAndGet();
    }
    
    public void decrementActiveTasks() {
        activeTasks.decrementAndGet();
    }
    
    public long getActiveTasks() {
        return activeTasks.get();
    }
    
    public void setActiveTasks(long count) {
        activeTasks.set(count);
    }
    
    public void incrementCompletedTasks() {
        completedTasks.incrementAndGet();
    }
    
    public long getCompletedTasks() {
        return completedTasks.get();
    }
    
    public void incrementFailedTasks() {
        failedTasks.incrementAndGet();
    }
    
    public long getFailedTasks() {
        return failedTasks.get();
    }
    
    public void setCpuUsage(long usage) {
        cpuUsage.set(usage);
    }
    
    public long getCpuUsage() {
        return cpuUsage.get();
    }
    
    @Override
    public String toString() {
        return "ResourceUsage{" +
                "threadCount=" + threadCount.get() +
                ", queueSize=" + queueSize.get() +
                ", activeTasks=" + activeTasks.get() +
                ", completedTasks=" + completedTasks.get() +
                ", failedTasks=" + failedTasks.get() +
                ", cpuUsage=" + cpuUsage.get() + "%" +
                '}';
    }
}
